package com.jar.exercise.反射.任务1;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * 创建Mytxt类，创建myCreate()方法完成创建文件D:\myhello.txt文件的功能。
 * 创建带main方法的NewInstanceTest类，通过Class类获取Mytxt对象，调用myCreat()
 */
public class Mytxt {
    public void creatTxt() {
        File file = new File("d:/reflection.txt");
        try {
            boolean newFile = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Class<Mytxt> mytxtClass = Mytxt.class;
            Mytxt mytxt = mytxtClass.newInstance();
            Method creatTxt = mytxtClass.getDeclaredMethod("creatTxt");
            creatTxt.invoke(mytxt, null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
